{{- $isRunningOnServer := false -}}
{{- $inputIsAbsolute := eq (substr (.Get "href") 0 1) "/" -}}
{{- if eq (substr (.Get "href" | absURL) 0 4) "http" -}}
  {{- $isRunningOnServer = true -}}
{{- end -}}

{{- $href := .Get "href" -}}
{{- $outputRelUrl := printf "%s" $href -}}
{{- $serverRootDir := "" -}}

{{- if not (or (eq (substr $href 0 2) "/#") (eq (substr $href 0 1) "#") (eq (substr $href 0 4) "http")) -}}

  {{ $foundDot := false }}
  {{- range (split (.Get "href") "/") -}}
    {{ $foundDot = false }}
    {{- if in . "." -}}
      {{ $foundDot = true }}
    {{- end -}}
  {{- end -}}

  {{- if $foundDot -}}  {{/* Linked files */}}
    {{ if not (fileExists $href) }}
      {{- errorf "in '%s': Could not resolve link '%s'. The linked file does not exist." .Page.File.Path $href -}}
    {{- end -}}
  {{- else -}}  {{/* Linked pages */}}
    {{- $hasHash := false -}}
    {{- $relUrlWithoutHash := replace (printf "/%s" (.Get "href")) "//" "/" -}}
    {{- $hash := "" -}}
    {{- if in $href "#" -}}
      {{- $hasHash = true -}}
      {{- $index := 0 -}}
      {{- range (split $href "#") -}}
        {{- if eq $index 0 -}}
          {{- $relUrlWithoutHash = . -}}
        {{- else if eq $index 1 -}}
          {{- $hash = printf "#%s" . -}}
        {{- end -}}
        {{- $index = add $index 1 -}}
      {{- end -}}
    {{- end -}}

    {{- $inputRelUrl := $href | relURL -}}
    {{- if in $inputRelUrl .Site.Data.mosaic.serverRootDir -}}
      {{- $inputRelUrl = strings.TrimPrefix .Site.Data.mosaic.serverRootDir $inputRelUrl -}}
    {{- end -}}
    {{- $outputRelUrl = printf "%s" $inputRelUrl -}}

    {{- if not (eq $relUrlWithoutHash "") -}}
      {{- $fileFound := false -}}
      {{- if eq (substr $relUrlWithoutHash -1 1) "/" -}}
        {{- $indexMdPath := replace (printf "/%s%s" $relUrlWithoutHash "index.md") "//" "/" -}}
        {{- $_indexMdPath := replace (printf "/%s%s" $relUrlWithoutHash "_index.md") "//" "/" -}}
        {{- $filenameMdPath_cutDash := replace (printf "/%s%s" (substr $relUrlWithoutHash 0 -1) ".md") "//" "/" -}}

        {{- if fileExists $indexMdPath -}}
          {{- $fileFound = true -}}
          {{- $outputRelUrl = printf "%s%s" $relUrlWithoutHash $hash -}}
        {{- else if fileExists $_indexMdPath -}}
          {{- $fileFound = true -}}
          {{- $outputRelUrl = printf "%s%s" $relUrlWithoutHash $hash -}}
        {{- else if fileExists $filenameMdPath_cutDash -}}
          {{- $fileFound = true -}}
          {{- $outputRelUrl = printf "%s%s" (substr $relUrlWithoutHash 0 -1) $hash -}}
        {{- end -}}
      {{- else -}}
        {{- $indexMdPath_addedDash := replace (printf "/%s/%s" $relUrlWithoutHash "index.md") "//" "/" -}}
        {{- $_indexMdPath_addedDash := replace (printf "/%s/%s" $relUrlWithoutHash "_index.md") "//" "/" -}}
        {{- $filenameMdPath := replace (printf "/%s%s" $relUrlWithoutHash ".md") "//" "/" -}}

        {{- if fileExists $indexMdPath_addedDash -}}
          {{- $fileFound = true -}}
          {{- $outputRelUrl = printf "%s/%s" $relUrlWithoutHash $hash -}}
        {{- else if fileExists $_indexMdPath_addedDash -}}
          {{- $fileFound = true -}}
          {{- $outputRelUrl = printf "%s/%s" $relUrlWithoutHash $hash -}}
        {{- else if fileExists $filenameMdPath -}}
          {{- $fileFound = true -}}
          {{- $outputRelUrl = printf "%s%s" $relUrlWithoutHash $hash -}}
        {{- end -}}
      {{- end -}}

      {{- if not $fileFound -}}
        {{- $absolutePathMsg := "" -}}
        {{- if $inputIsAbsolute -}}
          {{- $absolutePathMsg = "Consider using an absolute path." -}}
        {{- end -}}
        {{- errorf "in '%s': Could not resolve link '%s'. The linked pages does not exist. %s" .Page.File.Path (.Get "href") $absolutePathMsg -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
{{- $resultHref := printf "%s" $outputRelUrl -}}
{{- if $isRunningOnServer -}}
  {{- $resultHref = printf "%s/%s" (strings.TrimSuffix "/" .Site.Data.mosaic.serverRootDir) (strings.TrimPrefix "/" $outputRelUrl) -}}
{{- end -}}
<a href="{{- $resultHref -}}" title="{{- .Get "title" -}}">{{- .Get "title" -}}</a>